﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Data.OleDb;


namespace App1
{
    public partial class Open : Form
    {
        public Open()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text.ToString() + ".txt";
            try
            {
                GameChess.OpenGame();
                // GameChess.NewGame();
                StreamReader Re = File.OpenText(textBox1.Text);
                for (int j = 0; j < 2; j++)
                {
                    string s;

                    // player 1
                    s = Re.ReadLine().ToString();
                    int i = int.Parse(s[0].ToString());

                    int d = 0, c = 0;
                    // pawns /2-3-4-5-6
                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    GameChess.Player[i].Pawns[0].OpenRow(d);
                    GameChess.Player[i].Pawns[0].OpenCol(c);

                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    GameChess.Player[i].Pawns[1].OpenRow(d);
                    GameChess.Player[i].Pawns[1].OpenCol(c);

                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    GameChess.Player[i].Pawns[2].OpenRow(d);
                    GameChess.Player[i].Pawns[2].OpenCol(c);

                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    GameChess.Player[i].Pawns[3].OpenRow(d);
                    GameChess.Player[i].Pawns[3].OpenCol(c);

                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    GameChess.Player[i].Pawns[4].OpenRow(d);
                    GameChess.Player[i].Pawns[4].OpenCol(c);

                    // Guards 7-8
                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    GameChess.Player[i].Guards[0].OpenRow(d);
                    GameChess.Player[i].Guards[0].OpenCol(c);


                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    GameChess.Player[i].Guards[1].OpenRow(d);
                    GameChess.Player[i].Guards[1].OpenCol(c);

                    // Elephants 9-10
                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    GameChess.Player[i].Elephants[0].OpenRow(d);
                    GameChess.Player[i].Elephants[0].OpenCol(c);

                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    GameChess.Player[i].Elephants[1].OpenRow(d);
                    GameChess.Player[i].Elephants[1].OpenCol(c);

                    // Horses 11-12
                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    GameChess.Player[i].Horses[0].OpenRow(d);
                    GameChess.Player[i].Horses[0].OpenCol(c);

                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    GameChess.Player[i].Horses[1].OpenRow(d);
                    GameChess.Player[i].Horses[1].OpenCol(c);

                    //Cannons 12-14
                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    GameChess.Player[i].Cannons[0].OpenRow(d);
                    GameChess.Player[i].Cannons[0].OpenCol(c);

                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    GameChess.Player[i].Cannons[1].OpenRow(d);
                    GameChess.Player[i].Cannons[1].OpenCol(c);

                    // Rooks 15-16
                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    GameChess.Player[i].Rooks[0].OpenRow(d);
                    GameChess.Player[i].Rooks[0].OpenCol(c);

                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    GameChess.Player[i].Rooks[1].OpenRow(d);
                    GameChess.Player[i].Rooks[1].OpenCol(c);

                    // King 17
                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    GameChess.Player[i].King.OpenRow(d);
                    GameChess.Player[i].King.OpenCol(c);
                }
                GameChess.UdateOpen();
                Re.Close();
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("Tên ván đấu bạn mở không tồn tại");
            }
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
